<?php
namespace app\common\model\newmodel;
use think\Model;
use app\common\model\newmodel\Accessories as AccessoryModel;
class Device extends Model
{
    protected $table = 'device';
    protected $pk = 'id';
    
    /**
     * 关联配件表
     */
    public function accessories()
    {
        return $this->hasMany('accessories','device_id');
    }
    /**
     * 关联工厂
     */
    public function factory()
    {
        return $this->belongsTo('app\\common\\model\\newmodel\\Factory','factory_id','id');
    }
    /**
     * 设备列表
     */
    public static function deviceList($factory_id='')
    {

        $query=self::with(['accessories','factory'])->where('id','>',1);
        if($factory_id){
            $query=$query->where('factory_id',$factory_id);
        }
        $query=$query->select()->toArray();
        return [
            'code' => 1,
            'msg' => 'success',
            'data' => $query
        ];
    }

    /**
     * 添加设备
     */
    public static function addDevice($data)
    {
        if (empty($data['device_name']) || empty($data['factory_id']) || !isset($data['is_need'])) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }

        try {
            $device = new self();
            $device->device_name = $data['device_name'];
            $device->factory_id = $data['factory_id'];
            $device->is_need = $data['is_need'];
            $device->save();
            $device_id=$device->id;
            if(!empty($data['accessories'])){
                $accessories=$data['accessories'];
                foreach($accessories as $accessory){
                    $accessoryData['accessory_name']=$accessory;
                    $accessoryData['device_id']=$device_id;
                    AccessoryModel::addAccessory($accessoryData);
                }
            }
            return ['code' => 1, 'msg' => '设备添加成功'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '设备添加失败：' . $e->getMessage()];
        }
    }


    /**
     * 修改设备
     */
    public static function updateDevice($data)
    {
        if (empty($data['id']) || empty($data['device_name']) || empty($data['factory_id']) || !isset($data['is_need'])) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }

        $device = self::get($data['id']);
        if (!$device) {
            return ['code' => 0, 'msg' => '设备不存在'];
        }

        try {
            $device->device_name = $data['device_name'];
            $device->factory_id = $data['factory_id'];
            $device->is_need = $data['is_need'];
            $device->save();
            $device_id=$device->id;
            if(!empty($data['deleted_accessories'])){
                AccessoryModel::whereIn('id',$data['deleted_accessories'])->delete();
            }
            if(!empty($data['accessories'])){
                $accessories=$data['accessories'];
                foreach($accessories as $accessory){
                    $accessoryData['accessory_name']=$accessory['accessory_name'];
                    $accessoryData['device_id']=$device_id;
                
                    if($accessory['action']=='add')
                        AccessoryModel::addAccessory($accessoryData);
                    if($accessory['action']=='update'){
                        $accessoryData['id']=$accessory['id'];
                        AccessoryModel::updateAccessory($accessoryData);
                    }
                        
                }
            }
            return ['code' => 1, 'msg' => '设备修改成功'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '设备修改失败：' . $e->getMessage()];
        }
    }

    /**
     * 删除设备
     */
    public static function deleteDevice($id)
    {
        if (empty($id)) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }

        $device = self::get($id);
        if (!$device) {
            return ['code' => 0, 'msg' => '设备不存在'];
        }

        try {
            $device_id=$device->id;
            $device->delete();
            AccessoryModel::where('device_id',$device_id)->delete();
            return ['code' => 1, 'msg' => '设备删除成功'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '设备删除失败：' . $e->getMessage()];
        }
    }
}